﻿using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Common.ViewModels.Behaviors.ProcessManagement
{
    /// <summary>
    /// Command to show the previous step
    /// </summary>
    public class PreviousStepCommand : ViewModelCommand
    {
        private readonly ProcessService _processService;

        public PreviousStepCommand(ProcessService processService)
        {
            _processService = processService;

            processService.NewPropertyObserver()
                .AddHandler("CurrentStep", c => OnCanExecuteChanged());
        }

        protected override void OnExecute(object param)
        {
            int currentIndex = _processService.Steps.IndexOf(_processService.CurrentStep);
            _processService.Show(currentIndex - 1);
        }

        protected override bool OnCanExecute(object param)
        {
            int currentIndex = _processService.Steps.IndexOf(_processService.CurrentStep);
            return (currentIndex > 0);
        }
    }
}